﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Threading.Tasks;
using Sern.Common.ExpressionSerializer;

namespace Sern.Common
{
    public class ClientSerializingParameterInspector : IParameterInspector
    {
        /// <summary>
        /// Serialized any expression passed into the call.
        /// </summary>
        /// <param name="operationName"></param>
        /// <param name="inputs"></param>
        /// <returns></returns>
        public object BeforeCall(string operationName, object[] inputs)
        {
            for(int i = 0 ; i< inputs.Length; i++)
            {
                if(inputs[i] is Expression)
                {
                    inputs[i] = new SerializedExpression
                                    {
                                        XmlExpression =
                                            ExpressionSerializerFactory.Create.Serialize(inputs[i] as Expression)
                                    };
                }
            }

            return Guid.NewGuid();
        }

        public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState){}
    }
}
